from flask import Blueprint, render_template, jsonify
from flask_login import current_user
from datetime import datetime
from app.models.user import User
from app.models.movie import Movie

# 创建蓝图
index_bp = Blueprint('index', __name__, url_prefix='')

@index_bp.route('/')
def index():
    """主页路由，直接渲染index.html模板"""
    return render_template('index.html', datetime=datetime)

@index_bp.route('/api/stats')
def get_stats():
    """获取系统统计数据的API"""
    if not current_user.is_authenticated:
        return jsonify({
            'success': False,
            'message': '请先登录'
        })
    
    try:
        # 获取用户统计信息
        total_users = User.query.count()
        
        # 获取电影统计信息
        total_movies = Movie.query.count()
        
        return jsonify({
            'success': True,
            'data': {
                'users': {
                    'total': total_users
                },
                'movies': {
                    'total': total_movies
                },
                'searches': {
                    'total': 0
                }
            }
        })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': str(e)
        }) 